package com.ruoyi.merchant.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.merchant.domain.MerchantPrinter;
import com.ruoyi.merchant.domain.dto.PrinterSubmitDTO;
import com.ruoyi.merchant.service.IMerchantPrinterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 打印机管理Controller
 */
@RestController
@RequestMapping("/merchant/printer")
public class MerchantPrinterController extends BaseController {

    @Autowired
    private IMerchantPrinterService printerService;

    /**
     * 查询打印机列表
     */
    @GetMapping("/list")
    public AjaxResult list() {
        List<MerchantPrinter> list = printerService.selectPrinterList();
        return success(list);
    }

    /**
     * 获取打印机详细信息
     */
    @GetMapping("/{printerId}")
    public AjaxResult getInfo(@PathVariable("printerId") Long printerId) {
        return success(printerService.selectPrinterByPrinterId(printerId));
    }

    /**
     * 新增打印机
     */
    @Log(title = "打印机管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PrinterSubmitDTO printerDTO) {
        return toAjax(printerService.insertPrinter(printerDTO));
    }

    /**
     * 更新打印机状态
     */
    @Log(title = "更新打印机状态", businessType = BusinessType.UPDATE)
    @PutMapping("/status/{printerId}")
    public AjaxResult updateStatus(
            @PathVariable Long printerId,
            @RequestBody Map<String, Integer> params) {
        Integer status = params.get("status");
        if (status == null || (status != 0 && status != 1 && status != 2)) {
            return error("状态值错误");
        }
        return toAjax(printerService.updatePrinterStatus(printerId, status));
    }

    /**
     * 修改打印机
     */
    @Log(title = "打印机管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PrinterSubmitDTO printerDTO) {
        return toAjax(printerService.updatePrinter(printerDTO));
    }

    /**
     * 删除打印机
     */
    @Log(title = "打印机管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{printerId}")
    public AjaxResult remove(@PathVariable Long printerId) {
        return toAjax(printerService.deletePrinterByPrinterId(printerId));
    }

    /**
     * 批量删除打印机
     */
    @Log(title = "打印机管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/batch-delete")
    public AjaxResult batchRemove(@RequestBody Map<String, Object> params) {
        Long[] printerIds = ((List<Integer>) params.get("printerIds"))
                .stream()
                .map(Long::valueOf)
                .toArray(Long[]::new);
        return toAjax(printerService.deletePrinterByPrinterIds(printerIds));
    }


    /**
     * 设置默认打印机
     */
    @Log(title = "设置默认打印机", businessType = BusinessType.UPDATE)
    @PutMapping("/set-default/{printerId}")
    public AjaxResult setDefault(@PathVariable Long printerId) {
        return toAjax(printerService.setDefaultPrinter(printerId));
    }

    /**
     * 测试打印
     */
    @Log(title = "测试打印", businessType = BusinessType.OTHER)
    @PostMapping("/test/{printerId}")
    public AjaxResult test(@PathVariable Long printerId) {
        return toAjax(printerService.testPrint(printerId));
    }
}
